package com.wanxi.servlet.other;

import com.wanxi.model.other.BrandModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.wanxi.servlet.other.ShowServlet.brandModelList;

@WebServlet("/addModel1")
public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("msg");
        BrandModel brandModel=new BrandModel();
        brandModel.setId(Integer.parseInt(req.getParameter("id")));
        brandModel.setEnterPrise(req.getParameter("enterPrise"));
        brandModel.setName(req.getParameter("name"));
        brandModel.setIntroduction(req.getParameter("introduction"));
        brandModel.setStatus(Integer.parseInt(req.getParameter("status")));
        for (BrandModel b:
             brandModelList) {
            if(b.getId().equals(brandModel.getId())){
                req.getSession().setAttribute("msg","id已存在");
                resp.sendRedirect("/add");
                return;
            }
        }
        brandModelList.add(brandModel);
        resp.sendRedirect("/show");
    }
}
